;================================================
;Mapper号
MAPPER_NUMBER               =   04
;================================================
;MMC3 (Mapper 4) 寄存器常量
MAPPER_REG_BANK_CTRL        =   $8000
MAPPER_REG_BANK_DATA        =   $8001
MAPPER_REG_MIRRORING        =   $A000
MAPPER_REG_PRG_RAM_PROTECT  =   $A001
MAPPER_REG_IRQ_LATCH        =   $C000
MAPPER_REG_IRQ_RELOAD       =   $C001
MAPPER_REG_IRQ_DISABLE      =   $E000
MAPPER_REG_IRQ_ENABLE       =   $E001
;================================================
;================================================
MACRO_MAPPER_INIT .MACRO
    STA MAPPER_REG_IRQ_DISABLE;禁用IRQ
    LDA #MAPPER_MIRRORING ^ $01
    STA MAPPER_REG_MIRRORING;命名表镜像设置
    ;初始化图形bank
    LDX #$05
.Chr_Bank_Init
    STX MAPPER_REG_BANK_CTRL
    LDA .Chr_Bank_Data,X
    STA MAPPER_REG_BANK_DATA
    DEX
    BPL .Chr_Bank_Init
    JMP .Chr_Bank_Init_End
.Chr_Bank_Data
    .DB $00,$02,$04,$05,$06,$07
.Chr_Bank_Init_End
    .ENDM

;================================================
;PRG RAM 操作
;------------------------------------------------
;启用 PRG RAM $6000-7FFF
MACRO_PRG_RAM_ENABLE .MACRO
    LDA #$80
    STA MAPPER_REG_PRG_RAM_PROTECT
    .ENDM

;------------------------------------------------
;禁用 PRG RAM $6000-7FFF
MACRO_PRG_RAM_DISABLE .MACRO
    LDA #$40
    STA MAPPER_REG_PRG_RAM_PROTECT
    .ENDM

;================================================
;切页操作
;------------------------------------------------
;切页模式设置为 8KB 切换到 $8000-$9FFF
MACRO_SWITCH_BANK_8000_MODE .MACRO
    LDA #$06
    STA MAPPER_REG_BANK_CTRL
    .ENDM

;------------------------------------------------
;切页模式设置为 8KB 切换到 $A000-$BFFF
MACRO_SWITCH_BANK_A000_MODE .MACRO
    LDA #$07
    STA MAPPER_REG_BANK_CTRL
    .ENDM

;------------------------------------------------
;设置切页Bank数据
MACRO_SWITCH_BANK_DATA .MACRO
    STA MAPPER_REG_BANK_DATA
    .ENDM

;------------------------------------------------
;切页 8KB 切换到 $8000-$9FFF
MACRO_SWITCH_BANK_8000_A .MACRO
    PHA
    LDA #$06
    STA MAPPER_REG_BANK_CTRL
    PLA
    STA MAPPER_REG_BANK_DATA
    .ENDM

;------------------------------------------------
;切页 8KB 切换到 $A000-$BFFF
MACRO_SWITCH_BANK_A000_A .MACRO
    PHA
    LDA #$07
    STA MAPPER_REG_BANK_CTRL
    PLA
    STA MAPPER_REG_BANK_DATA
    .ENDM

;================================================
;IRQ操作
;------------------------------------------------
;启用IRQ
MACRO_ENABLE_IRQ  .MACRO
    STA MAPPER_REG_IRQ_ENABLE
    .ENDM
 
;------------------------------------------------
;禁用IRQ
MACRO_DISABLE_IRQ  .MACRO
    STA MAPPER_REG_IRQ_DISABLE
    .ENDM
 
;------------------------------------------------
;响应IRQ
MACRO_ACK_IRQ .MACRO
    STA MAPPER_REG_IRQ_DISABLE
    STA MAPPER_REG_IRQ_ENABLE
    .ENDM
 